<?php
/**
 * Class that operate on table 'boleteria'. Database Mysql.
 *
 * @author: http://phpdao.com
 * @date: 2013-11-23 21:12
 */
class BoleteriaMySqlDAO implements BoleteriaDAO{

	/**
	 * Get Domain object by primry key
	 *
	 * @param String $id primary key
	 * @return BoleteriaMySql 
	 */
	public function load($id){
		$sql = 'SELECT * FROM boleteria WHERE id_boleta = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($id);
		return $this->getRow($sqlQuery);
	}

	/**
	 * Get all records from table
	 */
	public function queryAll(){
		$sql = 'SELECT * FROM boleteria';
		$sqlQuery = new SqlQuery($sql);
		return $this->getList($sqlQuery);
	}
	
	/**
	 * Get all records from table ordered by field
	 *
	 * @param $orderColumn column name
	 */
	public function queryAllOrderBy($orderColumn){
		$sql = 'SELECT * FROM boleteria ORDER BY '.$orderColumn;
		$sqlQuery = new SqlQuery($sql);
		return $this->getList($sqlQuery);
	}
	
	/**
 	 * Delete record from table
 	 * @param boleteria primary key
 	 */
	public function delete($id_boleta){
		$sql = 'DELETE FROM boleteria WHERE id_boleta = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($id_boleta);
		return $this->executeUpdate($sqlQuery);
	}
	
	/**
 	 * Insert record to table
 	 *
 	 * @param BoleteriaMySql boleteria
 	 */
	public function insert($boleteria){
		$sql = 'INSERT INTO boleteria (codigo_boleta, nro_boleta, tipo_boleta, costo_boleta, id_evento, id_deCompetencia, id_espacio, estado_boleta) VALUES (?, ?, ?, ?, ?, ?, ?, ?)';
		$sqlQuery = new SqlQuery($sql);
		
		$sqlQuery->set($boleteria->codigoBoleta);
		$sqlQuery->set($boleteria->nroBoleta);
		$sqlQuery->set($boleteria->tipoBoleta);
		$sqlQuery->set($boleteria->costoBoleta);
		$sqlQuery->setNumber($boleteria->idEvento);
		$sqlQuery->setNumber($boleteria->idDeCompetencia);
		$sqlQuery->setNumber($boleteria->idEspacio);
		$sqlQuery->set($boleteria->estadoBoleta);

		$id = $this->executeInsert($sqlQuery);	
		$boleteria->idBoleta = $id;
		return $id;
	}
	
	/**
 	 * Update record in table
 	 *
 	 * @param BoleteriaMySql boleteria
 	 */
	public function update($boleteria){
		$sql = 'UPDATE boleteria SET codigo_boleta = ?, nro_boleta = ?, tipo_boleta = ?, costo_boleta = ?, id_evento = ?, id_deCompetencia = ?, id_espacio = ?, estado_boleta = ? WHERE id_boleta = ?';
		$sqlQuery = new SqlQuery($sql);
		
		$sqlQuery->set($boleteria->codigoBoleta);
		$sqlQuery->set($boleteria->nroBoleta);
		$sqlQuery->set($boleteria->tipoBoleta);
		$sqlQuery->set($boleteria->costoBoleta);
		$sqlQuery->setNumber($boleteria->idEvento);
		$sqlQuery->setNumber($boleteria->idDeCompetencia);
		$sqlQuery->setNumber($boleteria->idEspacio);
		$sqlQuery->set($boleteria->estadoBoleta);

		$sqlQuery->setNumber($boleteria->idBoleta);
		return $this->executeUpdate($sqlQuery);
	}

	/**
 	 * Delete all rows
 	 */
	public function clean(){
		$sql = 'DELETE FROM boleteria';
		$sqlQuery = new SqlQuery($sql);
		return $this->executeUpdate($sqlQuery);
	}

	public function queryByCodigoBoleta($value){
		$sql = 'SELECT * FROM boleteria WHERE codigo_boleta = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}

	public function queryByNroBoleta($value){
		$sql = 'SELECT * FROM boleteria WHERE nro_boleta = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}

	public function queryByTipoBoleta($value){
		$sql = 'SELECT * FROM boleteria WHERE tipo_boleta = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}

	public function queryByCostoBoleta($value){
		$sql = 'SELECT * FROM boleteria WHERE costo_boleta = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}

	public function queryByIdEvento($value){
		$sql = 'SELECT * FROM boleteria WHERE id_evento = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($value);
		return $this->getList($sqlQuery);
	}

	public function queryByIdDeCompetencia($value){
		$sql = 'SELECT * FROM boleteria WHERE id_deCompetencia = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($value);
		return $this->getList($sqlQuery);
	}

	public function queryByIdEspacio($value){
		$sql = 'SELECT * FROM boleteria WHERE id_espacio = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($value);
		return $this->getList($sqlQuery);
	}

	public function queryByEstadoBoleta($value){
		$sql = 'SELECT * FROM boleteria WHERE estado_boleta = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}


	public function deleteByCodigoBoleta($value){
		$sql = 'DELETE FROM boleteria WHERE codigo_boleta = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByNroBoleta($value){
		$sql = 'DELETE FROM boleteria WHERE nro_boleta = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByTipoBoleta($value){
		$sql = 'DELETE FROM boleteria WHERE tipo_boleta = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByCostoBoleta($value){
		$sql = 'DELETE FROM boleteria WHERE costo_boleta = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByIdEvento($value){
		$sql = 'DELETE FROM boleteria WHERE id_evento = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByIdDeCompetencia($value){
		$sql = 'DELETE FROM boleteria WHERE id_deCompetencia = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByIdEspacio($value){
		$sql = 'DELETE FROM boleteria WHERE id_espacio = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByEstadoBoleta($value){
		$sql = 'DELETE FROM boleteria WHERE estado_boleta = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}


	
	/**
	 * Read row
	 *
	 * @return BoleteriaMySql 
	 */
	protected function readRow($row){
		$boleteria = new Boleteria();
		
		$boleteria->idBoleta = $row['id_boleta'];
		$boleteria->codigoBoleta = $row['codigo_boleta'];
		$boleteria->nroBoleta = $row['nro_boleta'];
		$boleteria->tipoBoleta = $row['tipo_boleta'];
		$boleteria->costoBoleta = $row['costo_boleta'];
		$boleteria->idEvento = $row['id_evento'];
		$boleteria->idDeCompetencia = $row['id_deCompetencia'];
		$boleteria->idEspacio = $row['id_espacio'];
		$boleteria->estadoBoleta = $row['estado_boleta'];

		return $boleteria;
	}
	
	protected function getList($sqlQuery){
		$tab = QueryExecutor::execute($sqlQuery);
		$ret = array();
		for($i=0;$i<count($tab);$i++){
			$ret[$i] = $this->readRow($tab[$i]);
		}
		return $ret;
	}
	
	/**
	 * Get row
	 *
	 * @return BoleteriaMySql 
	 */
	protected function getRow($sqlQuery){
		$tab = QueryExecutor::execute($sqlQuery);
		if(count($tab)==0){
			return null;
		}
		return $this->readRow($tab[0]);		
	}
	
	/**
	 * Execute sql query
	 */
	protected function execute($sqlQuery){
		return QueryExecutor::execute($sqlQuery);
	}
	
		
	/**
	 * Execute sql query
	 */
	protected function executeUpdate($sqlQuery){
		return QueryExecutor::executeUpdate($sqlQuery);
	}

	/**
	 * Query for one row and one column
	 */
	protected function querySingleResult($sqlQuery){
		return QueryExecutor::queryForString($sqlQuery);
	}

	/**
	 * Insert row to table
	 */
	protected function executeInsert($sqlQuery){
		return QueryExecutor::executeInsert($sqlQuery);
	}
}
?>